
#include "Board.h"

//Constructor
GameBoard::GameBoard()
{
	dimensionX = 20;
	dimensionY = 20;
	gameStart();
}

void GameBoard::getBoardStatus() // will print board to screen
{
	
}

int GameBoard::makeShot(int x, int y, int pID)
{
	//check playerID and that player's board in order to make sure that they have not already shot there before
	int safe = 0;
	Coordinate *attemptCoords = new Coordinate(x, y);  // this will need to be used if there is a hit or if there's a miss in order to add it to the list of missed coords
	cout << "Made attempt coords.\n";
	if(player1Board.size() != 0) {
	if(pID == 1) 
	{
	cout << "test \n";

		for(std::list<Coordinate>::iterator listIterator = player1Board.begin(); listIterator != player1Board.end(); listIterator++)
		{
			cout << "test2 \n";
			if(listIterator->getX() == x && listIterator->getY() == y)
			{
				cout << "test 3\n";
				safe = -1;
				cout << "Already shot there. \n";
			}
		}
	}
	else if(pID == 2)
	{
		for(std::list<Coordinate>::iterator listIterator = player2Board.begin(); listIterator != player2Board.end(); listIterator++)
		{
			if(listIterator->getX() == x && listIterator->getY() == y)
			{
				safe = -1;
				cout << "Already shot there. \n";
			}
		}
	}
	}

	if(safe == 0)  // shot is OK
	{

		//check to make sure they are not shooting their own ship
		if(pID ==1)
		{	cout << "test \n";	// traverse each linked list (fleet and ship)
			for(std::list<Ship>::iterator fleetIterator = player1Fleet.begin(); fleetIterator != player1Fleet.end(); fleetIterator++)
			{	cout << "test2 \n";
				for(std::list<Coordinate>::iterator shipIterator = fleetIterator->getCoords().begin(); shipIterator != fleetIterator->getCoords().end(); ++shipIterator)
				{	cout << "test3 \n";
					cout << shipIterator->getX();
					if(shipIterator->getX() == x && shipIterator->getY() == y) //HIT
					{	cout << "test4 \n";
						if(shipIterator->getX() == x && shipIterator->getY() == y)
						{
							safe = -1;
							cout << "You are shooting your own ship. \n";
						}
					}
				}
			}
		}
		else if(pID == 2)
		{
			for(std::list<Ship>::iterator fleetIterator = player2Fleet.begin(); fleetIterator != player2Fleet.end(); fleetIterator++)
			{
				for(std::list<Coordinate>::iterator shipIterator = fleetIterator->getCoords().begin(); shipIterator != fleetIterator->getCoords().end(); shipIterator++)
				{
					if(shipIterator->getX() == x && shipIterator->getY() == y) //HIT
					{
						safe = -1;
						cout << "You are shooting your own ship. \n";
					}
				}
			}			
			
		}
		if(safe == 0)
		{
			cout << "test1";
			safe = -1;  // we want to test for hits now.

			//compare shot location to opponent's fleet location for hits
			if(pID ==2)
			{		// traverse each linked list (fleet and ship)
				for(std::list<Ship>::iterator fleetIterator = player1Fleet.begin(); fleetIterator != player1Fleet.end(); fleetIterator++)
				{
					for(std::list<Coordinate>::iterator shipIterator = fleetIterator->getCoords().begin(); shipIterator != fleetIterator->getCoords().end(); shipIterator++)
					{
						if(shipIterator->getX() == x && shipIterator->getY() == y) //HIT
						{
							cout << "Hit! \n";
							cout << "You have hit the other player's " << fleetIterator->getName() << " at (" << x << ", " << y << ").";
							safe = 0;
							fleetIterator->hit(shipIterator, player2);  // register hit with ship against player 1
						}
					}
				}
				if(safe == -1)
				{
					cout << "You have missed.";
					player1Board.push_back(*attemptCoords);  // add the attempted shot coords to the list of missed shots for the player.
				}
			}
			else if(pID == 1)
			{
				for(std::list<Ship>::iterator fleetIterator = player2Fleet.begin(); fleetIterator != player2Fleet.end(); fleetIterator++)
				{
					for(std::list<Coordinate>::iterator shipIterator = fleetIterator->getCoords().begin(); shipIterator != fleetIterator->getCoords().end(); shipIterator++)
					{
						if(shipIterator->getX() == x && shipIterator->getY() == y) //HIT
						{
							cout << "Hit! \n";
							cout << "You have hit the other player's " << fleetIterator->getName() << " at (" << x << ", " << y << ").";
							safe = 0;
							fleetIterator->hit(shipIterator, player1);  // register hit with ship
						}
					}
					
				}
				if(safe == -1)
				{
					cout << "You have missed.";
					player2Board.push_back(*attemptCoords);  // add the attempted shot coords to the list of missed shots for the player.
				}

			}

		}
	}
	else  //shooting in the same place as before. Ask for new coords.
	{
		cout << "Please choose a new location to shoot at.";
		return -1;
	}
	
	return -1;
}

int GameBoard::gameOverStatus()
{
	if(player1->getShipsRemaining() == 0 || player2->getShipsRemaining() == 0)
	{
		cout << "Game Over";
		return 0;
	}
	else
	{
		return -1;
	}
}

void GameBoard::gameStart()
{
	// This will initialize each player, their boards and fleets, 
	// First, initialize each player
	
	player1 = new Player(1);
	player2 = new Player(2);
	
	//Initialization of the playerBoards is already done
	
	//Initialize the fleet of ships for each player
	//Player 1
	for(int i = 0; i < 5; i++)
	{
		//ask player to place ship location
		int xStart;
		int yStart;
		int startDirection;
		
		cout << "Please enter X coordinate for ship position \n";
		//cin >> xStart;
		cout << "Please enter Y coordinate for ship position \n";
		//cin >> yStart;
		cout << "Choose direction for ship.  0: up, 1: right, 2: down, 3: left";
		//cin >> startDirection;
		//Code for checking the validity of ship placement and position




		xStart = 0;  // for debugging; default values
		yStart = i;
		startDirection = 1;
		
		
		//Convert to Coordinate struct and make ship at location
		Coordinate *startCoords = new Coordinate(xStart, yStart);
		player1Fleet.push_back(*(new Ship(i, *startCoords, startDirection)));
		cout << "Ship " << i << " placed. \n";
	}
	
	//Player 2
	for(int i = 0; i < 5; i++)
		{
		//ask player to place ship location
				int xStart;
				int yStart;
				int startDirection;
				
				cout << "Please enter X coordinate for ship position \n";
				//cin >> xStart;
				cout << "Please enter Y coordinate for ship position \n";
				//cin >> yStart;
				cout << "Choose direction for ship.  0: up, 1: right, 2: down, 3: left";
				//cin >> startDirection;
				//Code for checking the validity of ship placement and position
				
				
				
				xStart = 0;
				yStart = i+10;
				startDirection = 1;

				//Convert to Coordinate struct and make ship at location
				Coordinate *startCoords = new Coordinate(xStart, yStart);
				player2Fleet.push_back(*(new Ship(i, *startCoords, startDirection)));
				cout << "Ship " << i << " placed. \n";
		}
	
	
}


